package com.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.dto.Dto;
import com.bean.pojo.ItripUser;
import com.bean.token.Token;
import com.bean.vo.ItripTokenVO;
import com.dao.ItripUserMapper;
import com.service.IItripUserService;
import com.service.TokenService;
import com.util.DtoUtil;
import com.util.MD5;
import com.util.RedisUtil;
import com.util.UserAgentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class LoginController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IItripUserService iItripUserService;
    @Autowired
    private TokenService tokenService;

    private int expire = 7200;
    private String login_prefix = "login:";


    @PostMapping("/api/dologin")
    public Dto<ItripUser> Login(String name, String password,HttpServletRequest request){
        System.out.println(">>> 用户 --> 登录");
        System.out.println(name+" "+password);
        //获取用户的设备信息
        String agent = request.getHeader("user-agent");
        System.out.println("agent:" + agent);

        /* 判断, 该用户是否已经登录 */
        if (redisUtil.exists(login_prefix + name)){
            //获取token
            String token = redisUtil.getString(login_prefix + name);
            //获取token中的设备类型
            String type1 = token.substring(token.indexOf(":")+1, token.indexOf("-"));
            //获取本次请求的设备类型
            String type2 = "pc";
            if (UserAgentUtil.CheckAgent(agent)){
                type2 = "mobile";
            }
            //比对设备类型是否一致
            if (type1.equalsIgnoreCase(type2)){
                return DtoUtil.returnSuccess("登录成功！",new ItripTokenVO(token, expire*1000+new Date().getTime(),new Date().getTime()));
            }
        }

        /* 登录查询 */
        QueryWrapper<ItripUser> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode",name);
        wrapper.eq("userPassword", MD5.getMd5(password,32));

        ItripUser loginInfo = iItripUserService.getOne(wrapper);

        System.out.println(loginInfo);

        /* 根据查询结果处理 */
        /* 登录失败 */
        if (loginInfo == null){
            return DtoUtil.returnError("用户名或密码错误!");
        }

        /* 登录成功 */
        //生成token
        String token = tokenService.generate(loginInfo, agent);
        System.out.println("生成token"+token);
        //保存token
        tokenService.save(token, loginInfo, agent);
        //保存登录状态
        redisUtil.setString(login_prefix + name, token,expire);

        return DtoUtil.returnSuccess("登录成功！",new ItripTokenVO(token, expire*1000+new Date().getTime(),new Date().getTime()));
    }


    @GetMapping("/api/logout")
    public Dto logout(HttpServletRequest request){
        System.out.println(">>> 用户 --> 注销");

        //获取token字符串
        StringBuffer token = new StringBuffer();
        token.append(request.getHeader("token"));

        System.out.println(token);

        String string = redisUtil.getString(token.toString());

        if(string!=null){

            redisUtil.setString(token.toString(),string,3);

            //获取用户名
            String key = token.substring(token.indexOf("-")+1);
            System.out.println(key);

            key = key.substring(0, key.indexOf("-"));

            System.out.println(key);

            redisUtil.setString(login_prefix+key,"null",3);

            return DtoUtil.returnSuccess("注销成功");
        }


        return DtoUtil.returnError("401","注销失败","null");
    }

}
